package org.edu.modules.logistics.base.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.utils.ConvertUtils;
import org.edu.modules.logistics.base.mapper.VeHqFileMapper;
import org.edu.modules.logistics.base.entity.VeHqFile;
import org.edu.modules.logistics.base.dto.VeHqFileDto;
import org.edu.modules.logistics.base.service.VeHqFileService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 实现类
 */
@Service
public class VeHqFileServiceImpl extends BaseServiceImpl<VeHqFile, VeHqFileMapper> implements VeHqFileService {

    public IPage<VeHqFileDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper){
        return mapper.pageDto(page,queryWrapper);
    }
    /**
    * 查询实体类
    */
    public VeHqFileDto findDtoById(String id) {
        VeHqFile entity = super.findById(id);
        VeHqFileDto dto = ConvertUtils.convertToDto(entity, VeHqFileDto.class);
        return dto;
    }

    /**
    * 新增或者修改
    */
    public void insertOrUpdate(VeHqFileDto dto, Boolean isAdd){
        if(isAdd){
            super.insert(dto);
        }else{
            super.update(dto);
        }
    }

    @Override
    public void updateFiles(List<Integer> fileIds, String serviceId, String serviceType) {
        //删除之前搬定的
        LambdaUpdateWrapper<VeHqFile> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(VeHqFile::getServiceId,serviceId);
        wrapper.eq(VeHqFile::getServiceType,serviceType);
        if(CollUtil.isNotEmpty(fileIds)){
            wrapper.notIn(VeHqFile::getId,fileIds);
        }
        mapper.delete(wrapper);
        //
        if(CollUtil.isNotEmpty(fileIds)){
            LambdaUpdateWrapper<VeHqFile> wrapperUpdate = new LambdaUpdateWrapper<>();
            wrapperUpdate.set(VeHqFile::getServiceId,serviceId);
            wrapperUpdate.in(VeHqFile::getId,fileIds);
            mapper.update(null,wrapperUpdate);
        }

    }

}
